package com.dormitory.dao.impl;

import com.dormitory.dao.RoomDao;
import com.dormitory.model.Room;
import com.dormitory.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class RoomDaoImpl implements RoomDao {
    
    @Override
    public boolean save(Room room) {
        String sql = "INSERT INTO rooms (building_id, room_number, capacity, current_occupancy, room_status) " +
                    "VALUES (?, ?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, room.getBuildingId());
            stmt.setString(2, room.getRoomNumber());
            stmt.setInt(3, room.getCapacity());
            stmt.setInt(4, room.getCurrentOccupancy());
            stmt.setString(5, room.getRoomStatus());
            
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(Room room) {
        String sql = "UPDATE rooms SET building_id=?, room_number=?, capacity=?, " +
                    "current_occupancy=?, room_status=? WHERE room_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, room.getBuildingId());
            stmt.setString(2, room.getRoomNumber());
            stmt.setInt(3, room.getCapacity());
            stmt.setInt(4, room.getCurrentOccupancy());
            stmt.setString(5, room.getRoomStatus());
            stmt.setInt(6, room.getRoomId());
            
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete(Integer roomId) {
        String sql = "DELETE FROM rooms WHERE room_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, roomId);
            return stmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Room findById(Integer roomId) {
        String sql = "SELECT * FROM rooms WHERE room_id=?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, roomId);
            ResultSet rs = stmt.executeQuery();
            
            if (rs.next()) {
                return extractRoomFromResultSet(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Room> findAll() {
        String sql = "SELECT * FROM rooms";
        List<Room> rooms = new ArrayList<>();
        
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                rooms.add(extractRoomFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rooms;
    }

    @Override
    public List<Room> findByBuildingId(Integer buildingId) {
        String sql = "SELECT * FROM rooms WHERE building_id=?";
        List<Room> rooms = new ArrayList<>();
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            stmt.setInt(1, buildingId);
            ResultSet rs = stmt.executeQuery();
            
            while (rs.next()) {
                rooms.add(extractRoomFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rooms;
    }

    @Override
    public List<Room> findByCondition(String condition) {
        String sql = "SELECT * FROM rooms WHERE room_number LIKE ? OR room_status LIKE ?";
        List<Room> rooms = new ArrayList<>();
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            
            String searchPattern = "%" + condition + "%";
            stmt.setString(1, searchPattern);
            stmt.setString(2, searchPattern);
            
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                rooms.add(extractRoomFromResultSet(rs));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rooms;
    }

    private Room extractRoomFromResultSet(ResultSet rs) throws SQLException {
        Room room = new Room();
        room.setRoomId(rs.getInt("room_id"));
        room.setBuildingId(rs.getInt("building_id"));
        room.setRoomNumber(rs.getString("room_number"));
        room.setCapacity(rs.getInt("capacity"));
        room.setCurrentOccupancy(rs.getInt("current_occupancy"));
        room.setRoomStatus(rs.getString("room_status"));
        return room;
    }
} 